#include "EvalBleedThroughSubstitutionGrowing.hpp"
#include <math.h>


EvalBleedThroughSubstitutionGrowing::EvalBleedThroughSubstitutionGrowing(const girl::tools::Image &verso)
    : EvalBleedThroughAdd(verso)
{}



float EvalBleedThroughSubstitutionGrowing::evaluate(const girl::tools::Image &image)
{
    BleedThroughParameters *params = dynamic_cast<BleedThroughParameters * >(_params);
    //girl::tools::Image out(image);

    /*for(int i = 0; i< image.width(); i++)
    {
        for(int j = 0; j< image.height(); j++)
        {
            girl::tools::Pixel p = image.getPixel(i,j);
            out.setPixel(i,j,p);

        }
    }*/

    int seuil = _thresholder.getRectoThreshold(image,getVerso(),_seuil);

    std::cout <<  " seuil " << seuil << std::endl;

    GlobalThresholdBinarisation rectoBinarisation(image,seuil);
    girl::regionDeque * rectoRegions = rectoBinarisation.underThdRegions();
    girl::toger * rectoToger = rectoBinarisation.toger();
    std::cout << "Hello "<< std::endl;



    std::cout << " recto SIZE of regions Under : " << rectoRegions->size() << std::endl;
    std::cout << " recto SIZE of regions: " << rectoBinarisation.regions()->size() << std::endl;


    girl::regionDeque::iterator rectoIt = rectoRegions->begin();
    girl::regionDeque::iterator rectoItEnd = rectoRegions->end();


    int nb_sub =0;
    int growing = 0;
    int areas = girl::tools::computeArea(*rectoToger,*rectoIt);
    int nbAreas = 1;
    int nbPointsCompRecto = 0;
    for(; rectoIt != rectoItEnd; ++rectoIt)
    {

        //std::cout << *rectoIt << std::endl;
        bool in = false;
        girl::region r_recto = *rectoIt;
        girl::point p1;
        girl::point p2;
        girl::tools::computeBoundingBox(*rectoToger,r_recto,p1,p2);

        //std::cout << p1.x() << " " << p2.x()  << std::endl;

        int rectoCurrentArea = girl::tools::computeArea(*rectoToger,*rectoIt);

        if(rectoCurrentArea > 20 * (areas/nbAreas) /100){


            girl::regionDeque::iterator versoIt = _versoRegions->begin();
            girl::regionDeque::iterator versoItEnd = _versoRegions->end();


            for(; versoIt != versoItEnd && !in; ++versoIt)
            {

                girl::region r_verso = *versoIt;

                std::vector<girl::point> points;
                _versoToger->regionDomain(r_verso,points);
                //  std::cout << "Nb Points on region " << r_recto << ": " << points.size() << std::endl;


                int currentArea = girl::tools::computeArea(*_versoToger,*versoIt);
                areas+= currentArea;
                nbAreas++;


                if(currentArea > 20 * (areas/nbAreas) /100){

                    for(int i = 0; i<points.size() && !in;i++)
                    {
                        in = (points[i].x() >= p1.x() && points[i].x() <= p2.x()
                              && points[i].y() >= p1.y() && points[i].y() <= p2.y());
                    }
                }
                //std::cout << "in : " << in << std::endl;

            }
            if(in){

                girl::point p1_verso;
                girl::point p2_verso;
                girl::tools::computeBoundingBox(*_versoToger,*versoIt,p1_verso,p2_verso);

                std::vector<girl::point> points;
                rectoToger->regionDomain(r_recto,points);


                std::vector<girl::point> points2;
                _versoToger->regionDomain(*versoIt,points2);
                int nb_points =  points.size() + points2.size();
                nbPointsCompRecto += points.size();
                for(int i = 0; i < points2.size(); i++){
                    for(int j = 0; j < points.size(); j++)
                    {
                        if(points[j].x() == points2[i].x()
                            && points[j].y() == points2[i].y())
                            {
                            //out.setPixel(points[j].x(),points[j].y(), girl::tools::Pixel(0,0,255));
                            nb_points--;
                        }else{

                            //out.setPixel(points[j].x(),points[j].y(), girl::tools::Pixel(255,0,0));
                        }
                    }
                }
                growing += nb_points;
                nb_sub++;


            }
        }

    }

/*    ImageConverter converter;
    QImage image1 = converter.convert(out);
    QImageWriter writer1("log.png", "png");
    writer1.write(image1);
    std::cout << "fin" << std::endl;*/

    //ATTENTION DOCUMENTER le rectoregions
    float tc = float(growing) / float(nb_sub);
    float cir = float(nbPointsCompRecto) / float(nb_sub);

    float mesure = tc/cir;

    std::cout << "nb " << growing << std::endl;
    std::cout << "NBSubGroswingEVAL " << mesure << std::endl;

    return mesure;
}
